package com.xianguo.datacenter.serviceImpl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.swing.filechooser.FileSystemView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xianguo.datacenter.domain.CenterFile;
import com.xianguo.datacenter.domain.CenterFolder;
import com.xianguo.datacenter.domain.Disk;
import com.xianguo.datacenter.download.Thread.Download.DownloadState;
import com.xianguo.datacenter.download.Thread.DownloadUtil;
import com.xianguo.datacenter.mapping.CommodityTypeMapper;
import com.xianguo.datacenter.mapping.CommonMapper;
import com.xianguo.datacenter.service.CommonService;

@Service("CommonService")
public class CommonServiceImpl implements CommonService {
	
	@Autowired
	private CommonMapper commonMapper;
	
	@Override
	public List<Disk> getDiskList() {
		FileSystemView fileSys = FileSystemView.getFileSystemView();
		List<Disk> disks = new ArrayList<Disk>();
		File[] roots = File.listRoots();
		int i = 0;
		for (File f : File.listRoots()) {
			Disk disk = new Disk();
			disk.setName(fileSys.getSystemDisplayName(f));// 获取系统卷标及名字
			disk.setType(fileSys.getSystemTypeDescription(f));// 获取系统卷的类型
			disk.setSize(f.getTotalSpace());// 获取该卷大小(单位：字节)
			disk.setFreeSize(f.getFreeSpace());// 获取该卷可用大小(单位：字节)
			disk.setSymbol(roots[i].toString().replaceAll("\\\\", "\\\\\\\\"));
			disks.add(disk);
			i++;
		}
		return disks;
	}

	@Override
	public CenterFolder getFolder(String path) {
		CenterFolder centerFolder = new CenterFolder();// 文件夹对象
		centerFolder.setPath(path); // 当前文件夹路径
		File file = new File(path); // file对象
		File[] tempList = file.listFiles(); // 拿到当前路径所有文件和文件夹
		centerFolder.setName(file.getName()); // 当前文件家名称
		List<CenterFile> cfs = new ArrayList<CenterFile>();
		List<CenterFolder> cfos = new ArrayList<CenterFolder>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		for (File item : tempList) {
			if (item.isFile()) {
				CenterFile cf = new CenterFile();
				cf.setPath(item.getPath().replaceAll("\\\\", "\\\\\\\\"));
				cf.setSize(item.length());
				cf.setName(item.getName());
				cf.setType(item.getPath().substring(item.getPath().lastIndexOf(".") + 1));
				cf.setCreatTime(sdf.format(new Date(item.lastModified())));
				cf.setId(UUID.randomUUID().toString());
				cfs.add(cf);
			} else if (item.isDirectory()) {
				CenterFolder cf = new CenterFolder();
				cf.setName(item.getName());
				cf.setPath(item.getPath().replaceAll("\\\\", "\\\\\\\\"));
				/*
				 * CenterFolder superFolder = new CenterFolder();
				 * superFolder.setName(item.getParentFile().getName());
				 * superFolder.setPath(item.getParentFile().getPath());
				 * cf.setSuperFolder(superFolder);
				 */
				cfos.add(cf);
			}
		}
		centerFolder.setFiles(cfs);
		centerFolder.setFolders(cfos);
		File superFile = file.getParentFile();
		if (superFile != null) {
			CenterFolder superFolder = new CenterFolder();
			superFolder.setName(file.getName());
			superFolder.setPath(superFile.getPath().replaceAll("\\\\","\\\\\\\\"));
			centerFolder.setSuperFolder(superFolder);
		} else {
			centerFolder.setSuperFolder(null);
		}
		return centerFolder;
	}

	@Override
	public void AddHttpdownload(String url, String saveDir, String fileName) {
		DownloadUtil.addHttpDownload(saveDir, fileName, url,null);
	}

	@Override
	public Map<String, String> addBaiDuYunDownload(String url, String saveDir,
			String webPath) {
		Map<String, String> map = DownloadUtil.addBaiDuYunDownload(saveDir, url, webPath);
		return map;
	}

	@Override
	public Map<String, String> setBaiDuYunVCode(String UID, String VCode) {
		Map<String, String> map = DownloadUtil.setBaiDuYunVCode(UID, VCode);
		return map;
	}

	@Override
	public void changeDownloadState(String UID, DownloadState isdownloading) {
		if(isdownloading == DownloadState.DOWNLOADING || isdownloading == DownloadState.CONTINUE){
			DownloadUtil.changeDownloadState(DownloadState.SUSPEND, UID);
		}else if(isdownloading == DownloadState.SUSPEND){
			DownloadUtil.changeDownloadState(DownloadState.CONTINUE, UID);
		}
	}

	@Override
	public void insertFile(CenterFile file) {
		commonMapper.insertFile(file);
	}

	@Override
	public void deleteAllFile() {
		commonMapper.deleteAllFile();
	}

	@Override
	public void insertType() {
		commonMapper.insertType();
	}

	@Override
	public List<Map<String, Object>> selectType() {
		return commonMapper.selectType();
	}
	
}
